package com.lucy.rabc.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.lucy.common.converter.JpaColumnConverter;
import com.lucy.common.converter.JpaListLongConverter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.Hibernate;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author kindear
 */
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "sys_dept")
@EntityListeners(AuditingEntityListener.class)
@ApiModel(value = "系统部门实体",description = "系统部门管理")
public class SysDept implements Serializable {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @ApiModelProperty(value = "部门ID",name = "id")
    Long id;

    /**
     * 父部门ID
     */
    @JsonIgnoreProperties(value = {"parent","children"})
    @ApiModelProperty("父菜单ID")
    @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=true)
    @JoinColumn(name="parent_id",updatable = false)
    SysDept parent;


    /**
     * 部门名称
     */
    @ApiModelProperty("部门名称")
    @Column(name = "dept_name")
    String deptName;


    /** 显示顺序 */
    @ApiModelProperty("显示顺序")
    @Column(name = "order_num")
    private Integer orderNum;

    /** 负责人 */
    @ApiModelProperty("负责人")
    @Column(name = "leader")
    private String leader;

    /** 联系电话 */
    @ApiModelProperty("联系电话")
    @Column(name = "phone")
    private String phone;

    /** 邮箱 */
    @ApiModelProperty("负责人邮箱")
    @Column(name = "email")
    private String email;

    /** 部门状态:1正常,0停用 */
    @ApiModelProperty("部门状态")
    @Column(name = "status")
    private Integer status;

    /** 子部门 */
    @JsonIgnoreProperties(value = {"parent"})
    @OneToMany(mappedBy = "parent",cascade=CascadeType.REMOVE,fetch=FetchType.LAZY)
    @ToString.Exclude
    Set<SysDept> children;


    @CreatedDate
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @ApiModelProperty(value = "记录创建时间",name = "createdTime")
    @Column(name = "created_time")
    private Date createdTime;
    @LastModifiedDate
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @ApiModelProperty(value = "记录更新时间",name = "updatedTime")
    @Column(name = "updated_time")
    private Date updatedTime;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        SysDept sysDept = (SysDept) o;
        return id != null && Objects.equals(id, sysDept.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
